package com.decent.ejfadmin.user.dao;


import com.decent.ejfadmin.common.bean.AdminInterface;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.user.dao.provider.AdminInterfaceDaoProvider;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 交接班
 *
 * @author weicx
 * @date 2018/07/26
 */
@Mapper
@Repository
public interface AdminInterfaceDao {
    /**
     * 新增交接班
     *
     * @param adminInterface 交接班内容
     * @return
     */
    @Insert("insert into YUHUANG_ADMINWORK (ADMIN_NAME,SEND_TIME,TITLE,CONTENT,TYPE,VALIDITY,ADMIN_ID,ADMIN_TYPE,BIND_ROLE_IDS)  " +
            "values(#{adminInterface.adminName},now(),#{adminInterface.title},#{adminInterface.content},#{adminInterface.type}," +
            "#{adminInterface.validity},#{adminInterface.adminId},#{adminInterface.adminType},#{adminInterface.bindRoleIds}) ")
    int insertContent(@Param("adminInterface") AdminInterface adminInterface);

    /**
     * 查询弹出的交班信息 查询所有有效期内的
     *
     * @param adminType 角色
     * @return
     */
    @Select("select id,ADMIN_NAME adminName,TITLE,CONTENT,TYPE,DATE_FORMAT(VALIDITY,'%Y-%m-%d %H:%i:%S') VALIDITY,read_admin readAdmin," +
            "DATE_FORMAT(SEND_TIME,'%Y-%m-%d %H:%i:%S')  sendTime,UPDATE_TIME updateTime \n" +
            "  from YUHUANG_ADMINWORK\n" +
            " where VALIDITY >now() " +
            "and Instr(BIND_ROLE_IDS,#{adminType} )>0  " +
            " order by TYPE desc,SEND_TIME DESC")
    List<AdminInterface> queryTitle(@Param("adminType") Integer adminType);

    /**
     * 查询交班列表
     *
     * @param page           页数信息
     * @param beginTime      开始时间
     * @param endTime        结束时间
     * @param adminInterface 交接内容
     * @return
     */
    @SelectProvider(type = AdminInterfaceDaoProvider.class, method = "queryTitleList")
    List<AdminInterface> queryTitleList(@Param("page") Page page, @Param("startTime") String beginTime,
                                        @Param("endTime") String endTime, @Param("adminInterface") AdminInterface adminInterface);

    /**
     * 查询条数
     *
     * @param beginTime      开始时间
     * @param endTime        结束时间
     * @param adminInterface 用户名
     * @return
     */
    @SelectProvider(type = AdminInterfaceDaoProvider.class, method = "queryTitleListCount")
    int queryTitleListCount(@Param("startTime") String beginTime, @Param("endTime") String endTime,
                            @Param("adminInterface") AdminInterface adminInterface);

    /**
     * 查询交班内容
     *
     * @param id 交接班主键
     * @return
     */
    @Select("select * from YUHUANG_ADMINWORK where ID =#{id}")
    AdminInterface queryContentById(@Param("id") Integer id);

    /**
     * 标记已读
     *
     * @param adminInterface 交接内容实体
     * @return
     */
    @Update("update YUHUANG_ADMINWORK set READ_ADMIN = #{adminInterface.readAdmin} where id = #{adminInterface.id}")
    int updateReadAdmin(@Param("adminInterface") AdminInterface adminInterface);

    /**
     * 修改交接内容
     *
     * @param adminInterface 交接实体
     * @return
     */
    @Update("update YUHUANG_ADMINWORK set " +
            "TITLE = #{adminInterface.title}," +
            "CONTENT =#{adminInterface.content}," +
            "UPDATE_TIME = now()," +
            "UPDATE_ADMIN = #{adminInterface.updateAdmin}," +
            "TYPE = #{adminInterface.type}," +
            "VALIDITY = #{adminInterface.validity}," +
            "BIND_ROLE_IDS=#{adminInterface.bindRoleIds} " +
            "where id = #{adminInterface.id}")
    int updateContent(@Param("adminInterface") AdminInterface adminInterface);

    /**
     * 根据角色ID获取角色名称
     *
     * @param roleIds
     * @return
     */
    @Select("select name from tbl_shiro_role where id in (${roleIds}) ")
    List<String> queryBindRoleNameByRoleId(@Param("roleIds") String roleIds);
}
